<%@ page language="java" contentType= "text/html" pageEncoding="UTF-8"%>
<%@ page import="com.jm.web.form.TableToolbar" %>
<%@ page import="com.jm.web.form.Button" %>
<%@ page import="com.jm.web.form.Toolbar" %>
<%@ page import="com.jm.web.form.Empty" %>
<%@include file="/include/common/griduihead1.jsp"%>
<%@include file="/include/common/griduihead2.jsp"%>
<%
	page1.setWelcome(false);//禁用欢迎信息
	page1.getPageTitle().setTitle("数据选择页面");
	page1.getToolbar().setEnab(false);//禁用工具栏
	page1.setMinWidth(200);
%>
<jm:form>
<jm:layout postion="north">
	<jm:toolbar newEnab="false" saveEnab="false" cancelEnab="false" deleteEnab="false" copyEnab="false" startEnab="false" checkEnab="false" exportEnab="false" printEnab="false" helpEnab="false"/>
	<jm:pageTitle />
	<jm:error></jm:error>
</jm:layout>

<jm:layout postion="center" export="true">
	<jm:table id="grid1">
		<jm:thead >
			<tr>
				<td width="50px"><keep>選<input type="checkbox" name="allselect" id="allselect" value="1" onclick="$('[name=itemselect]').attr('checked',this.checked);$('[name=groups]').attr('checked',this.checked);" /></keep></td>
			</tr>
		</jm:thead>
		<jm:tbody>
			
		</jm:tbody>
		<jm:tfoot/>
	</jm:table>
</jm:layout>

<jm:layout postion="south">
	<%
		TableToolbar toolbar = new TableToolbar();
		toolbar.InitTag(null);
		toolbar.getAddRowButton().setEnab(false);
		toolbar.init();
		toolbar.setLeft(false);
		
		Button calBtn = Toolbar.getNewButton("取消操作","okbtn","okbtn","ax-btn-icon sx-tb-cancel",null);
		calBtn.setWidth("100px");
		calBtn.setOnClick("top.close();");
		Button emptyBtn = Toolbar.getNewButton("清空選擇","okbtn","okbtn","ax-btn-icon sx-tb-restart",null);
		emptyBtn.setWidth("100px");
		emptyBtn.setOnClick("fun_empty();");
		Button okBtn = Toolbar.getNewButton("確定選擇","okbtn","okbtn","ax-btn-icon sx-tb-ok",null);
		okBtn.setWidth("100px");
		okBtn.setOnClick("fun_ok();");
		
		Empty empty = new Empty();
		empty.setWidth("100%");
		toolbar.getItems().clear();
		toolbar.getItems().add(empty);
		toolbar.getItems().add(calBtn);
		toolbar.getItems().add(emptyBtn);
		toolbar.getItems().add(okBtn);
	%>
	<div align="right">
	<%toolbar.onRender(out);%>
	</div>
</jm:layout>
</jm:form>
<jm:script><script type="text/javascript" charset="UTF-8">
	var config = top.dialogArguments;
	top.returnValue = {}; 
	
	function fun_empty(){
		if('table' == config['type']){
			top.returnValue['action'] = 'set';
		}else{
			top.returnValue['action'] = 'clear';
		}
		top.close();
	}
	
	function fun_ok(){
		top.returnValue['action'] = 'set';
		var value = '';
		var text = '';
		var bfirst = true;
		var split = $P.gv(config['split'],',');
		var valueAy = [];
		var textAy = [];
		
		if('table' == config['type']){
			$('[name=itemselect]:checked').each(function(){
				valueAy.push($(this).val());
				textAy.push($(this).attr('text'));
			});
			
			top.returnValue['value'] = valueAy;
			top.returnValue['text'] = textAy;
		}else{
			$('[name=itemselect]:checked').each(function(){
				if($P.gv($(this).val()) != ''){
					if(!bfirst){
						value += split;
						text += split;
					}else{
						bfirst = false;
					}
					value += $(this).val();
					text += $(this).attr('text');
				}
			});
			top.returnValue['value'] = value;
			top.returnValue['text'] = text;
		}
		top.close();
	}
	
	function ts(arg){
		if(arg == undefined)
			return '';
		for(var i=0;i<10;i++){
			var streg = '\\\%'+(i+1)+'\\\$s';
			arg = arg.replace(new RegExp(streg,'gm'),'{'+i+'}');
		}
		return arg;
	}
	
	$(function(){
		if(config){
			var cols = config['cols'];
			var loop = $P.gv(config['loop'],1);//循环几次
			
			if(cols){
				var ths = ""; 
				var colCount = 1;
				for(var j = 0;j<loop;j++){
					for(var i =0;i<cols.length;i++){
						var col = cols[i];
						if(col['hidden'] != true){
							if(j > 0){
								ths+=("<th>&nbsp;</th>");
								colCount++;
							}
							colCount++;
							ths+=("<th>"+$P.gv(col['caption'])+"</th>");
						}
					}
				}
				$('#grid1 thead tr').append(ths);
			}
			
			var data = config['data'];
			var split = $P.gv(config['split'],',');
			var valueconfig = {format:'{0}',params:'value'};
			var textconfig = {format:'{0}',params:'text'};
			
			$.extend(valueconfig,config['valueconfig']);
			$.extend(textconfig,config['textconfig']);
			valueconfig['format'] = ts(valueconfig['format']);
			textconfig['format'] = ts(textconfig['format']);
			
			var value = split+$P.gv(config['value']);
			
			
			var group = $P.gv(config['group']);//分組的欄位
			var groupMap = {};
			var gpindex = 0;
			var epnodesplit = $P.gv(config['epnodesplit'],',');
			var epnode = config['epnode']+epnodesplit;//需要展开的节点的值
			
			var trHtm = $("<div></div>"); 
			if(data && data.length > 0){
				for(var i=0;i<data.length;i++){
					var row = data[i];
					//tr
					var tr = $("<tr></tr>")
					for(var j = 0;j<loop;j++){
						if(j>0){
							i++;
							if(i>=data.length){break;}
							row = data[i];
						}
						var chk = $("<td><input type='checkbox' name='itemselect' value='' /></td>");
						//處理checkbox 的值
						var _value = '';
						var temp = [];
						for(var k =0 ;k<valueconfig.params.split(',').length;k++){
							temp.push($P.gv(row[valueconfig.params.split(',')[k]]));
						}
						_value += $.validator.format(valueconfig.format,temp);
						chk.find('input').val(_value);
						
						_value = split+_value+split;
						if((value+split).indexOf(_value) >= 0)
							chk.find('input').attr('checked',true);
						
						//處理checkbox 的text
						temp = [];
						for(var k =0 ;k<textconfig.params.split(',').length;k++){
							temp.push($P.gv(row[textconfig.params.split(',')[k]]));
						}
						chk.find('input').attr('text',$.validator.format(textconfig.format,temp));
						tr.append(chk);
						
						if(cols){
							for(var k =0;k<cols.length;k++){
								var col = cols[k];
								if(col['hidden'] != true){
									tr.append("<td>"+$P.gv(row[col['id']])+"</td>");
								}
							}
						}
					}
					var bdisplay = true;
					//group
					if(group != ''){
						bdisplay = false;
						var strgroup = $P.gv(row[group]);
						if(epnode != '' && (epnode == ('all'+epnodesplit) || epnode.indexOf(strgroup+epnodesplit) >=0 ))
							bdisplay = true;
							
						if((''+$P.gv(groupMap[strgroup])) == ''){
							groupMap[strgroup] = gpindex;
							gpindex++;
							var strTd = "<img onclick=\"$(this).toggleClass('ax-tree-elbow-minus');$('[id=group_"+gpindex+"]').toggle();$P.refresh();\" class=\"ax-tree-elbow-plus "+(bdisplay ? 'ax-tree-elbow-minus' : '')+"\" src=\"/jmPro/include/images/default/s.gif\" style=\"cursor: pointer;\" width=\"16px\" height=\"16px\" /> ";
							strTd+=" <input type='checkbox' name='groups' value='' onclick=\"$('[id=group_"+gpindex+"]').find('[name=itemselect]').attr('checked',this.checked);\"/>";
							strTd+=strgroup;
							var groupTr = $("<tr><td colspan=\""+colCount+"\">"+strTd+"</td></tr>")
							trHtm.append(groupTr);
							//$('#grid1 tbody').append(groupTr);
						}
					}
					
					if(!bdisplay){
						tr.css('display','none');
					}
					
					
					tr.attr('id','group_'+gpindex);
					//$('#grid1 tbody').append(tr);
					trHtm.append(tr);
				}
			}
			$('#grid1 tbody').html(trHtm.html());
		}
	});
</script></jm:script>
<%@include file="/include/common/griduiend.jsp"%>